﻿using Swimj.CommonLib.Base;
using Swimj.Core.Providers;
using Swimj.Core.Utilities;

namespace Admin.ApplicationLib.Services
{
    public class FileService : IFileService
    {
        private readonly IFileProvider _fileProvider;

        public FileService(IFileProvider fileProvider)
        {
            _fileProvider = fileProvider;
        }

        public async Task<Result<string>> UploadAsync(Stream stream, string bucketName, string fileName, string endpoint)
        {
            var extensionName = FileHelper.GetExtensionName(fileName);

            var contentType = FileHelper.GetContentType(extensionName);
            var newFileName = $"{Guid.NewGuid().ToString().Replace("-", "")}{extensionName}";
            await _fileProvider.UploadFile(stream, bucketName, newFileName, contentType);

            return await Result<string>.SuccessAsync($"http://{endpoint}/{bucketName}/{newFileName}", string.Empty);
        }
    }
}